home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / MATHTOOL.C < prev    next >
C/C++ Source or Header  |  1992-07-19  |  15KB  |  549 lines

  1. /* MathTools.c was written by:
  2.  
  3.       Mark C. Peterson
  4.       The Yankee Programmer
  5.       405-C Queen Street, Suite #181
  6.       Southington, CT 06489
  7.       (203) 276-9721
  8.  
  9.    If you make any changes to this file, please comment out the older
  10.    code with your name, date, and a short description of the change.
  11.  
  12.    Thanks!
  13.  
  14.                                  -Mark
  15.  
  16. */
  17.  
  18. #include <windows.h>
  19. #include "winfract.h"
  20. #include "mathtool.h"
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include "profile.h"
  24.  
  25. static char MTClassName[] =     "FFWMathTools";
  26. static char MTWindowTitle[] =   "Math Tools";
  27. HWND hFractalWnd, hMathToolsWnd, hCoordBox, hZoomDlg, hZoomBar;
  28. HANDLE hThisInst, hZoomBrush, hZoomPen;
  29. long FAR PASCAL MTWndProc();
  30. int MTWindowOpen = 0, CoordBoxOpen = 0, KillCoordBox = 0;
  31. int ZoomBarOpen = 0, KillZoomBar = 0, Zooming = 0, TrackingZoom = 0;
  32. POINT ZoomClick, ZoomCenter;
  33. int ZoomBarMax = 100, ZoomBarMin = -100;
  34. int Sizing = 1, ReSizing = 0;
  35.  
  36. static double Pi =  3.14159265359;
  37. FARPROC DefZoomProc;
  38.  
  39. RECT ZoomRect;
  40.  
  41. WORD CoordFormat = IDM_RECT;
  42. WORD AngleFormat = IDM_DEGREES;
  43.  
  44. extern unsigned xdots, ydots;
  45. extern double xxmin, yymin, xxmax, yymax, delxx, delyy;
  46. extern int ytop, ybottom, xleft, xright, ZoomMode;
  47. extern BOOL zoomflag;
  48. extern int time_to_restart, time_to_cycle, time_to_reinit, calc_status;
  49. extern int win_xoffset, win_yoffset;
  50.  
  51. void XorZoomBox(void);
  52.  
  53. extern int win_xoffset, win_yoffset;   /* BDT 11/6/91 */
  54.  
  55.  
  56. /* Global Maintenance */
  57.  
  58. void CheckMathTools(void) {
  59.    if(KillCoordBox) {
  60.       KillCoordBox = 0;
  61.       SaveParamSwitch(CoordBoxStr, FALSE);
  62.       CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_UNCHECKED);
  63.       DestroyWindow(hCoordBox);
  64.    }
  65.    if(KillZoomBar) {
  66.       XorZoomBox();
  67.       Zooming = 0;
  68.       KillZoomBar = 0;
  69.       SaveParamSwitch(ZoomBoxStr, FALSE);
  70.       CheckMenuItem(GetMenu(hFractalWnd), IDM_ZOOM, MF_UNCHECKED);
  71.       DestroyWindow(hZoomDlg);
  72.       CheckMenuItem(GetMenu(hFractalWnd), ZoomMode, MF_CHECKED);
  73.    }
  74. }
  75.  
  76. BOOL RegisterMathWindows(HANDLE hInstance) {
  77.     WNDCLASS  wc;
  78.  
  79.     SetToolsPath();
  80.     hThisInst = hInstance;
  81.  
  82.     wc.style = CS_OWNDC;
  83.     wc.lpfnWndProc = MTWndProc;
  84.     wc.cbClsExtra = 0;
  85.     wc.cbWndExtra = 0;
  86.     wc.hInstance = hInstance;
  87.     wc.hIcon = LoadIcon(hInstance, "MathToolIcon");
  88.     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  89.     wc.hbrBackground = GetStockObject(BLACK_BRUSH);
  90.     wc.lpszMenuName =  "MathToolsMenu";
  91.     wc.lpszClassName = MTClassName;
  92.  
  93.     return (RegisterClass(&wc));
  94. }
  95.  
  96.  
  97. /* Window Sizing */
  98.  
  99. void SizeWindow(HWND hWnd) {
  100.    if(Sizing) {
  101.       POINT w;
  102.  
  103.       w.x = xdots + (GetSystemMetrics(SM_CXFRAME) * 2);
  104.       w.y = ydots + (GetSystemMetrics(SM_CYFRAME) * 2) +
  105.                      GetSystemMetrics(SM_CYCAPTION) +
  106.                      GetSystemMetrics(SM_CYMENU);
  107.       ReSizing = 1;
  108.       ShowScrollBar(hWnd, SB_BOTH, FALSE);
  109.       SetWindowPos(hWnd, GetNextWindow(hWnd, GW_HWNDPREV), 0, 0,
  110.                    w.x, w.y, SWP_NOMOVE);
  111.       ReSizing = 0;
  112.    }
  113. }
  114.  
  115. void ReSizeWindow(HWND hWnd) {
  116.    if(Sizing) {
  117.       SizeWindow(hWnd);
  118.       ShowWindow(hWnd, SW_SHOWNA);
  119.    }
  120. }
  121.  
  122. void WindowSizing(HWND hWnd) {
  123.    if(Sizing) {
  124.       CheckMenuItem(GetMenu(hWnd), IDM_SIZING, MF_UNCHECKED);
  125.       Sizing = 0;
  126.       ShowScrollBar(hWnd, SB_BOTH, TRUE);
  127.    }
  128.    else {
  129.       CheckMenuItem(GetMenu(hWnd), IDM_SIZING, MF_CHECKED);
  130.       Sizing = 1;
  131.       ReSizeWindow(hWnd);
  132.    }
  133.    ProgStr = Winfract;
  134.    SaveParamSwitch(WindowSizingStr, Sizing);
  135. }
  136.  
  137.  
  138.  
  139. /* Zoom Box */
  140.  
  141. void XorZoomBox(void) {
  142.    HDC hDC;
  143.    HANDLE hOldBrush, hOldPen;
  144.    int OldMode;
  145.  
  146.    if(Zooming) {
  147.       hDC = GetDC(hFractalWnd);
  148.  
  149.       hOldBrush = SelectObject(hDC, hZoomBrush);
  150.       hOldPen = SelectObject(hDC, hZoomPen);
  151.       OldMode = SetROP2(hDC, R2_XORPEN);
  152.  
  153.       Rectangle(hDC, ZoomRect.left, ZoomRect.top, ZoomRect.right,
  154.                 ZoomRect.bottom);
  155.  
  156.       SelectObject(hDC, hOldBrush);
  157.       SelectObject(hDC, hOldPen);
  158.       SetROP2(hDC, OldMode);
  159.  
  160.       ReleaseDC(hFractalWnd, hDC);
  161.   }
  162. }
  163.  
  164. void PositionZoomBar(void) {
  165.    char Temp[15];
  166.  
  167.    SetScrollPos(hZoomBar, SB_CTL, Zooming, TRUE);
  168.    sprintf(Temp, "%d%%", Zooming);
  169.    SetDlgItemText(hZoomDlg, ID_PERCENT, Temp);
  170. }
  171.  
  172. void CalcZoomRect(void) {
  173.    unsigned Midx, Midy;
  174.    double z;
  175.  
  176.    if(Zooming) {
  177.       Midx = xdots >> 1;
  178.       Midy = ydots >> 1;
  179.       z = 1.0 - (fabs((double)Zooming / 100) * .99);
  180.       ZoomRect.left = ZoomCenter.x - (unsigned)(z * Midx) - 1;
  181.       ZoomRect.right = ZoomCenter.x + (unsigned)(z * Midx) + 1;
  182.       ZoomRect.top = ZoomCenter.y - (unsigned)(z * Midy) - 1;
  183.       ZoomRect.bottom = ZoomCenter.y + (unsigned)(z * Midy) + 1;
  184.    }
  185. }
  186.  
  187. void CenterZoom(void) {
  188.    ZoomCenter.x = xdots >> 1;
  189.    ZoomCenter.y = ydots >> 1;
  190. }
  191.  
  192. void StartZoomTracking(DWORD lp) {
  193.    HRGN hRgn;
  194.  
  195.    ZoomClick = MAKEPOINT(lp);
  196.    if(hRgn = CreateRectRgn(ZoomClick.x-1, ZoomClick.y-1, ZoomClick.x+1,
  197.                ZoomClick.y+1)) {
  198.       if(RectInRegion(hRgn, &ZoomRect))
  199.          TrackingZoom = TRUE;
  200.       DeleteObject(hRgn);
  201.    }
  202. }
  203.  
  204. void TrackZoom(DWORD lp) {
  205.    POINT NewPoint;
  206.  
  207.    XorZoomBox();
  208.  
  209.    NewPoint = MAKEPOINT(lp);
  210.    ZoomCenter.x += NewPoint.x - ZoomClick.x;
  211.    ZoomCenter.y += NewPoint.y - ZoomClick.y;
  212.    ZoomClick = NewPoint;
  213.  
  214.    CalcZoomRect();
  215.    XorZoomBox();
  216. }
  217.  
  218. void EndZoom(DWORD lp) {
  219.    TrackZoom(lp);
  220.    TrackingZoom = FALSE;
  221. }
  222.  
  223. void PaintMathTools(void) {
  224.    XorZoomBox();
  225. }
  226.  
  227. void CancelZoom(void) {
  228.    XorZoomBox();
  229.    CenterZoom();
  230.    TrackingZoom = Zooming = FALSE;
  231.    PositionZoomBar();
  232. }
  233.  
  234. void ExecuteZoom(void) {
  235.    double xd, yd, z;
  236.  
  237.    xd = xxmin + (delxx * (ZoomCenter.x + win_xoffset));  /* BDT 11/6/91 */
  238.    yd = yymax - (delyy * (ZoomCenter.y + win_yoffset));  /* BDT 11/6/91 */
  239.  
  240.    z = 1.0 - fabs((double)Zooming / 100 * .99);
  241.    if(Zooming > 0)
  242.       z = 1.0 / z;
  243.  
  244.    xxmin = xd - (delxx * z * (xdots / 2));
  245.    xxmax = xd + (delxx * z * (xdots / 2));
  246.    yymin = yd - (delyy * z * (ydots / 2));
  247.    yymax = yd + (delyy * z * (ydots / 2));
  248.  
  249.    time_to_reinit = 1;
  250.    time_to_cycle = 0;
  251.    calc_status = 0;
  252. }
  253.  
  254. BOOL FAR PASCAL ZoomBarProc(HWND hWnd, WORD Message, WORD wp, DWORD dp) {
  255.    switch(Message) {
  256.       case WM_KEYDOWN:
  257.          switch(wp) {
  258.             case VK_RETURN:
  259.                if(TrackingZoom)
  260.                   ExecuteZoom();
  261.                break;
  262.             case VK_ESCAPE:
  263.                CancelZoom();
  264.                break;
  265.             default:
  266.                break;
  267.          }
  268.          break;
  269.    }
  270.    return(CallWindowProc(DefZoomProc, hWnd, Message, wp, dp));
  271. }
  272.  
  273. BOOL FAR PASCAL ZoomBarDlg(HWND hDlg, WORD Message, WORD wp, DWORD dp) {
  274.    FARPROC lpFnct;
  275.  
  276.    switch(Message) {
  277.       case WM_INITDIALOG:
  278.          ZoomBarOpen = TRUE;
  279.          ProgStr = Winfract;
  280.          SaveParamSwitch(ZoomBoxStr, TRUE);
  281.          CenterZoom();
  282.          CheckMenuItem(GetMenu(hFractalWnd), IDM_ZOOM, MF_CHECKED);
  283.          PositionWindow(hDlg, ZoomBoxPosStr);
  284.          hZoomDlg = hDlg;
  285.          hZoomBrush = GetStockObject(BLACK_BRUSH);
  286.          hZoomPen = GetStockObject(WHITE_PEN);
  287.          if(!(lpFnct = MakeProcInstance(ZoomBarProc, hThisInst)))
  288.             return(FALSE);
  289.          if(!(hZoomBar = CreateWindow("scrollbar", NULL, WS_CHILD |
  290.                            WS_TABSTOP | SBS_VERT,
  291.                            38, 28, 18, 248, hDlg, NULL, hThisInst, NULL)))
  292.             return(FALSE);
  293.  
  294.          SetScrollRange(hZoomBar, SB_CTL, ZoomBarMin, ZoomBarMax, FALSE);
  295.          SetScrollPos(hZoomBar, SB_CTL, 0, FALSE);
  296.          ShowScrollBar(hZoomBar, SB_CTL, TRUE);
  297.          Zooming = 0;
  298.  
  299.          /* Create a Window Subclass */
  300.          DefZoomProc = (FARPROC)GetWindowLong(hZoomBar, GWL_WNDPROC);
  301.          SetWindowLong(hZoomBar, GWL_WNDPROC, (LONG)lpFnct);
  302.          return(TRUE);
  303.       case WM_MOVE:
  304.          SaveWindowPosition(hDlg, ZoomBoxPosStr);
  305.          break;
  306.       case WM_CLOSE:
  307.          KillZoomBar = 1;
  308.          ProgStr = Winfract;
  309.          break;
  310.       case WM_DESTROY:
  311.          ZoomBarOpen = 0;
  312.          break;
  313.       case WM_VSCROLL:
  314.          if(Zooming)
  315.             XorZoomBox();
  316.          switch(wp) {
  317.             case SB_PAGEDOWN:
  318.                Zooming += 20;
  319.             case SB_LINEDOWN:
  320.                Zooming = min(ZoomBarMax, Zooming + 1);
  321.                break;
  322.             case SB_PAGEUP:
  323.                Zooming -= 20;
  324.             case SB_LINEUP:
  325.                Zooming = max(ZoomBarMin, Zooming - 1);
  326.                break;
  327.             case SB_TOP:
  328.                Zooming = ZoomBarMin;
  329.                break;
  330.             case SB_BOTTOM:
  331.                Zooming = ZoomBarMax;
  332.                break;
  333.             case SB_THUMBPOSITION:
  334.             case SB_THUMBTRACK:
  335.                Zooming = LOWORD(dp);
  336.                break;
  337.          }
  338.          PositionZoomBar();
  339.          CalcZoomRect();
  340.          XorZoomBox();
  341.          break;
  342.       default:
  343.          break;
  344.    }
  345.    return(FALSE);
  346. }
  347.  
  348. void ZoomBar(HWND hWnd) {
  349.    FARPROC lpFnct;
  350.  
  351.    hFractalWnd = hWnd;
  352.    if(ZoomBarOpen)
  353.       KillZoomBar = TRUE;
  354.    else {
  355.       if(lpFnct = MakeProcInstance(ZoomBarDlg, hThisInst)) {
  356.          if(CreateDialog(hThisInst, "ZoomBar", hWnd, lpFnct))
  357.          {
  358.             SetFocus(hWnd);
  359.             return;
  360.          }
  361.       }
  362.       MessageBox(hWnd, "Error Opening Zoom Bar",
  363.                  NULL, MB_ICONEXCLAMATION | MB_OK);
  364.    }
  365. }
  366.  
  367.  
  368. /* Coordinate Box */
  369.  
  370. BOOL FAR PASCAL CoordBoxDlg(HWND hDlg, WORD Message, WORD wp, DWORD dp) {
  371.    HMENU hDlgMenu;
  372.  
  373.    hDlgMenu = GetMenu(hDlg);
  374.    switch(Message) {
  375.       case WM_INITDIALOG:
  376.          CoordBoxOpen = 1;
  377.          ProgStr = Winfract;
  378.          SaveParamSwitch(CoordBoxStr, TRUE);
  379.          CheckMenuItem(GetMenu(hFractalWnd), IDM_COORD, MF_CHECKED);
  380.          hCoordBox = hDlg;
  381.          PositionWindow(hDlg, CoordBoxPosStr);
  382.          return(TRUE);
  383.       case WM_MOVE:
  384.          SaveWindowPosition(hDlg, CoordBoxPosStr);
  385.          break;
  386.       case WM_CLOSE:
  387.          KillCoordBox = 1;
  388.          ProgStr = Winfract;
  389.          break;
  390.       case WM_DESTROY:
  391.          CoordBoxOpen = 0;
  392.          break;
  393.       case WM_COMMAND:
  394.          CheckMenuItem(hDlgMenu, AngleFormat, MF_UNCHECKED);
  395.          CheckMenuItem(hDlgMenu, CoordFormat, MF_UNCHECKED);
  396.          switch(wp) {
  397.             case IDM_RADIANS:
  398.             case IDM_GRAD:
  399.             case IDM_DEGREES:
  400.                AngleFormat = wp;
  401.                break;
  402.             case IDM_POLAR:
  403.             case IDM_RECT:
  404.             case IDM_PIXEL:
  405.                CoordFormat = wp;
  406.                break;
  407.          }
  408.          CheckMenuItem(hDlgMenu, AngleFormat, MF_CHECKED);
  409.          CheckMenuItem(hDlgMenu, CoordFormat, MF_CHECKED);
  410.          if(CoordFormat == IDM_POLAR) {
  411.             SetDlgItemText(hDlg, ID_X_NAME, "|z|");
  412.             SetDlgItemText(hDlg, ID_Y_NAME, "\xD8");
  413.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_ENABLED);
  414.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_ENABLED);
  415.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_ENABLED);
  416.          }
  417.          else {
  418.             SetDlgItemText(hDlg, ID_X_NAME, "x");
  419.             SetDlgItemText(hDlg, ID_Y_NAME, "y");
  420.             EnableMenuItem(hDlgMenu, IDM_DEGREES, MF_DISABLED | MF_GRAYED);
  421.             EnableMenuItem(hDlgMenu, IDM_RADIANS, MF_DISABLED | MF_GRAYED);
  422.             EnableMenuItem(hDlgMenu, IDM_GRAD, MF_DISABLED | MF_GRAYED);
  423.          }
  424.    }
  425.    return(FALSE);
  426. }
  427.  
  428. void UpdateCoordBox(DWORD dw) {
  429.    unsigned xPixel, yPixel;
  430.    double xd, yd, Angle, Modulus;
  431.    char xStr[40], yStr[40];
  432.  
  433.    xPixel = (unsigned)dw         + win_xoffset;  /* BDT 11/6/91 */
  434.    yPixel = (unsigned)(dw >> 16) + win_yoffset;  /* BDT 11/6/91 */
  435.    xd = xxmin + (delxx * xPixel);
  436.    yd = yymax - (delyy * yPixel);
  437.    switch(CoordFormat) {
  438.       case IDM_PIXEL:
  439.          sprintf(xStr, "%d", xPixel);
  440.          sprintf(yStr, "%d", yPixel);
  441.          break;
  442.       case IDM_RECT:
  443.          sprintf(xStr, "%+.8g", xd);
  444.          sprintf(yStr, "%+.8g", yd);
  445.          break;
  446.       case IDM_POLAR:
  447.          Modulus = (xd*xd) + (yd*yd);
  448.          if(Modulus > 1E-20) {
  449.             Modulus = sqrt(Modulus);
  450.             Angle = atan2(yd, xd);
  451.             switch(AngleFormat) {
  452.                case IDM_DEGREES:
  453.                   Angle = (Angle / Pi) * 180;
  454.                   break;
  455.                case IDM_GRAD:
  456.                   Angle = (Angle / Pi) * 200;
  457.                case IDM_RADIANS:
  458.                   break;
  459.             }
  460.          }
  461.          else {
  462.             Modulus = 0.0;
  463.             Angle = 0.0;
  464.          }
  465.          sprintf(xStr, "%+.8g", Modulus);
  466.          sprintf(yStr, "%+.8g", Angle);
  467.          break;
  468.    }
  469.    SetDlgItemText(hCoordBox, ID_X_COORD, xStr);
  470.    SetDlgItemText(hCoordBox, ID_Y_COORD, yStr);
  471. }
  472.  
  473. void CoordinateBox(HWND hWnd) {
  474.    FARPROC lpCoordBox;
  475.  
  476.    hFractalWnd = hWnd;
  477.    if(CoordBoxOpen)
  478.       KillCoordBox = TRUE;
  479.    else {
  480.       if(lpCoordBox = MakeProcInstance(CoordBoxDlg, hThisInst)) {
  481.          if(CreateDialog(hThisInst, "CoordBox", hWnd, lpCoordBox))
  482.             return;
  483.       }
  484.       MessageBox(hWnd, "Error Opening Coordinate Box",
  485.                  NULL, MB_ICONEXCLAMATION | MB_OK);
  486.    }
  487.    ProgStr = Winfract;
  488. }
  489.  
  490.  
  491.  
  492. /* Math Tools Window - Not Implemented Yet */
  493.  
  494. BOOL OpenMTWnd(void) {
  495.     hMathToolsWnd = CreateWindow(
  496.         MTClassName,
  497.         MTWindowTitle,
  498.         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
  499.         CW_USEDEFAULT, CW_USEDEFAULT,
  500.         xdots, ydots,
  501.         hFractalWnd,
  502.         NULL,
  503.         hThisInst,
  504.         NULL
  505.     );
  506.     if(!hMathToolsWnd)
  507.         return(FALSE);
  508.  
  509.     ShowWindow(hMathToolsWnd, SW_SHOWNORMAL);
  510.     UpdateWindow(hMathToolsWnd);
  511.  
  512.     return(TRUE);
  513. }
  514.  
  515. void MathToolBox(HWND hWnd) {
  516.     hFractalWnd = hWnd;
  517.     if(MTWindowOpen)
  518.        DestroyWindow(hMathToolsWnd);
  519.     else {
  520.       if(!OpenMTWnd())
  521.          MessageBox(hWnd, "Error Opening Math Tools Window", NULL,
  522.                     MB_ICONEXCLAMATION | MB_OK);
  523.     }
  524. }
  525.  
  526.  
  527. long FAR PASCAL MTWndProc(HWND hWnd, unsigned Message, WORD wParam, DWORD lParam) {
  528.    switch (Message) {
  529.       case WM_CREATE:
  530.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_CHECKED);
  531.          MTWindowOpen = 1;
  532.          break;
  533.       case WM_COMMAND:
  534.          switch(wParam) {
  535.             case IDM_EXIT:
  536.                DestroyWindow(hWnd);
  537.                break;
  538.          }
  539.          break;
  540.       case WM_DESTROY:
  541.          CheckMenuItem(GetMenu(hFractalWnd), IDM_MATH_TOOLS, MF_UNCHECKED);
  542.          MTWindowOpen = 0;
  543.          break;
  544.       default:
  545.          return(DefWindowProc(hWnd, Message, wParam, lParam));
  546.     }
  547.     return(0L);
  548. }
  549.